package com.shopping.service;

import java.util.List;

import com.shopping.DAO.UserDAO;
import com.shopping.exception.PasswordNotCorrectionException;
import com.shopping.exception.UsernameNotFoundException;
import com.shopping.user.User;

public class UserService {
	
	public static void save(User u) {
		 UserDAO.save(u);
	}
	/**
	 * @return所有User
	 */
	public static List<User> getUsers() {
		 return UserDAO.getUsers();
	}
	
	/**
	 * 登录
	 * 
	 * @date 2018-06-04
	 */
	public static User login(String username,String password) throws UsernameNotFoundException, PasswordNotCorrectionException {
		
		User u = UserDAO.findByUsername(username);
		if(u == null) {
			//用户不存在
			throw new UsernameNotFoundException();
		}
		
		if(!u.getPassword().equals(password)) {
			//密码不正确
			throw new PasswordNotCorrectionException();
		}
		
		return u;
	}
	
	
	/**
	 * 判断用户是否存在
	 * 
	 * @date 2018-06-04 
	 * @return true 存在
	 */
	public static boolean existUser(String username) {
		User u = UserDAO.findByUsername(username);
		if(u == null) {
			return false;
		}
		return true;
	}
	
	/*
	 * 修改个人信息
	 */
	public static void userChanged(User u,String username) {
		UserDAO.userChanged(u,username);
	}
	
	public static void delete(int id) {
		UserDAO.delete(id);
	}
}
